#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mount.h>
#include <error.h>

#define BUFFER_BYTES (2 * 1024 * 1024)

#define flush_buffer_cache() system("echo 3 > /proc/sys/vm/drop_caches")

int main(int argc, char *argv[])
{
	flush_buffer_cache();
	int open_flags = O_RDONLY | O_NONBLOCK;
	int fd = open(argv[1], open_flags);
	printf("%d\n", fd);
	char buf[BUFFER_BYTES];
	struct timeval begin, end;
	gettimeofday(&begin, NULL);
	int max_iter = 100;
	int opt_iter = 0;
	do
	{
		++opt_iter;
		read(fd, buf, BUFFER_BYTES);
	}while (opt_iter <= max_iter);

	gettimeofday(&end, NULL);
	double diff = (end.tv_sec - begin.tv_sec)*1000000.0 + (end.tv_usec - begin.tv_usec);
	printf("size: 200M; time: %5.6fs; speed: %5.2fMB/s\n", diff/1000000.0, 200.0 * 1000000.0/diff);
	close(fd);

	return 0;
}
